package  
{
	import net.flashpunk.graphics.Image;
	import net.flashpunk.World;
	import net.flashpunk.FP;
	import net.flashpunk.Sfx;
	/**
	 * ...
	 * @author Donatello
	 */
	public class FirstScene extends World
	{
		[Embed(source = "sfx/crash.mp3")]private const CRASH:Class;
		private var crash:Sfx;
		
		[Embed(source = "sfx/traffic.mp3")]private const TRAFFIC:Class;
		private var traffic:Sfx;
		
		[Embed(source = "img/road.png")]	private const IMG_ROAD:Class;
		private var imgRoad:Image;
		private var car:Car;
		
		public function FirstScene() 
		{
			imgRoad = new Image(IMG_ROAD);
			imgRoad.x = 0;
			imgRoad.y = 0;
			addGraphic(imgRoad);
			
			car = new Car(300,500);
			add(car);
			
			add(new Side(0, 0));
			add(new Side(700, 0));
			
			crash=new Sfx(CRASH)
			traffic = new Sfx(TRAFFIC);
			traffic.play();
			
			

		}
		
		override public function end():void
		{
			traffic.stop();
		}
		
		
		override public function update():void
		{
			if (FP.rand(500) < 10)
			{	
				if(FP.rand(100)>50)
					add(new EnemyCar(FP.rand(250) + 100,0,FP.rand(10)+1));
				else
					add(new EnemyCar(FP.rand(250) + 400,600,((FP.rand(10)*-1)-1)));
				
			}
			
			if (car.collide("EnemyCar", car.x, car.y)  || car.collide("Side", car.x, car.y))
			{
				crash.play();
				FP.world = new FirstScene();
				removeAll();
			}
			
			
			if (car.y + 71 <= 0)
			{	
				FP.world = new SecondScene();
				removeAll();
		
			}
				
			super.update();
		}
		
	}

}